package ru.tennisworld.user.mappers;

import org.mapstruct.BeanMapping;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import ru.tennisworld.user.config.MapstructConfig;
import ru.tennisworld.user.dto.RegistrationTennisUserRequestDto;
import ru.tennisworld.user.dto.TennisUserFullInfoResponseDto;
import ru.tennisworld.user.dto.TennisUserResponseDto;
import ru.tennisworld.user.entities.TennisUser;

import java.util.List;

@Mapper(config = MapstructConfig.class,
        uses = {EncryptMapper.class, TennisUserProfileMapper.class})
public interface TennisUserMapper {

    @Mapping(target = "login", source = "userLogin")
    @Mapping(target = "password", source = "userPassword", qualifiedByName = "encryptStringData")
    @Mapping(target = "tennisUserProfile", source = "userProfile", qualifiedByName = "requestProfileMapper")
    TennisUser mapToEntity(RegistrationTennisUserRequestDto dto);

    @Named("fullUserInfoMapper")
    @Mapping(target = "userInfo", source = "entity", qualifiedByName = "mainUserInfoMapper")
    @Mapping(target = "userProfileInfo", source = "tennisUserProfile", qualifiedByName = "responseProfileMapper")
    TennisUserFullInfoResponseDto mapToDto(TennisUser entity);

    @Named("mainUserInfoMapper")
    @Mapping(target = "userId", source = "id", qualifiedByName = "encryptLongData")
    @Mapping(target = "login", source = "login")
    TennisUserResponseDto mapToUserDto(TennisUser entity);


    @Mapping(target = "userId", expression = "java(entity.getId().toString())")
    @Mapping(target = "login", source = "login")
    TennisUserResponseDto mapToReferenceUserDto(TennisUser entity);

    @InheritConfiguration
    List<TennisUserResponseDto> mapToReferenceUserDto(List<TennisUser> entity);

    @BeanMapping(ignoreByDefault = true)
    @Mapping(target = "id")
    @Mapping(target = "login")
    @Mapping(target = "password")
    void updateTennisUser(TennisUser src, @MappingTarget TennisUser upd);
}
